/**
 * Created by xiaohai on 2018/4/9.
 */
$(document).ready(function() {
    $('.validator_form').bootstrapValidator().on('success.form.bv', function(e) {
        e.preventDefault();
        var $form = $(e.target);
        // 获得bootstrap验证对象
        var bv = $form.data('bootstrapValidator');
        // 使用Ajax提交form表单数据
        $.post($form.attr('action'), $form.serialize(), function(result) {
            setTimeout(function() {
                $(":submit", $form).removeAttr("disabled");
            }, 800);
            // console.log($(":submit", $form));
            if (result.code == 1) {
                bootoast({
                    message: result.msg,
                    type: 'success',
                    position: 'right-top',
                    timeout: 2
                });

                if (result.url && result.url.length > 2) {
                    setTimeout(function() {
                        if (result.data == "redirect") {
                            window.location.href = result.url;
                        } else {
                            var tab_html = $(".tab-pane", $("#main_tabs_content"));
                            tab_html.html("");
                            tab_html.load(result.url);
                        }
                    }, 1500);
                } else {

                }
            } else {
                bootoast({
                    message: result.msg,
                    type: 'danger',
                    position: 'right-top',
                    timeout: 2
                });
                if (result.url && result.url.length > 2) {
                    setTimeout(function() {
                        var tab_html = $(".tab-pane", $("#main_tabs_content"));
                        tab_html.html("");
                        tab_html.load(result.url);
                    }, 1500);
                }
            }


        }, 'json');

    });

    $(".select2").select2();

    $(".data_del_items").on("click", function() {
        var obj = $(this);
        var url = obj.attr("data-url");
        var id = obj.attr("param");
        var title = obj.attr("title");
        var target_item = obj.attr("target_item");
        var p_data = "id=" + id;
        if (target_item) {
            p_data = target_item + "=" + id;
        }

        console.log(url);
        del_item(url, p_data, title);
    })

    $('.datepicker').datepicker({
        language: 'zh-CN',
        autoclose: true
    });


    // ajax 请求
    $(".ajax_operation").on("click", function(e) {
        if (e && e.preventDefault) {
            e.preventDefault();
        } else {
            window.event.returnValue = false;
        }
        var obj = $(this);
        var url = obj.attr("href");
        var target_item = obj.attr("target_item");
        var target_id = obj.attr("data-" + target_item);
        $.post(url, target_item + "=" + target_id, function(result) {
            if (result.code == 1) {
                bootoast({
                    message: result.msg,
                    type: 'success',
                    position: 'right-top',
                    timeout: 2
                });
                if (result.url && result.url.length > 2) {
                    setTimeout(function() {
                        window.location.href = result.url;
                    }, 1500);
                } else {
                    setTimeout(function() {
                        window.location.reload();
                    }, 1500);
                }
            } else {
                bootoast({
                    message: result.msg,
                    type: 'danger',
                    position: 'right-top',
                    timeout: 2
                });
                if (result.url && result.url.length > 2) {
                    setTimeout(function() {
                        window.location.href = result.url;
                    }, 1500);
                }
            }
        }, "json");

    });

    $(".search_load_tab_form").bootstrapValidator().on('success.form.bv', function(e) {
        e.preventDefault();
        var $form = $(e.target);
        var tab_html = $(".tab-pane", $("#main_tabs_content"));
        tab_html.html("");
        var url = $form.attr("action");
        var data = $form.serialize();
        console.log(data);
        tab_html.load(url + "?" + data);

    });
});

$(".modal").on("hidden.bs.modal", function() {
    $(this).removeData("bs.modal");

});


function ajax_form_submit(form_id) {
    $('#' + form_id).bootstrapValidator().on('success.form.bv', function(e) {
        e.preventDefault();
        var $form = $(e.target);
        // 获得bootstrap验证对象
        var bv = $form.data('bootstrapValidator');
        // 使用Ajax提交form表单数据
        $.post($form.attr('action'), $form.serialize(), function(result) {
            setTimeout(function() {
                $(":submit", $form).removeAttr("disabled");
            }, 800);

            // console.log($(":submit", $form));
            if (result.code == 1) {
                bootoast({
                    message: result.msg,
                    type: 'success',
                    position: 'right-top',
                    timeout: 2
                });
                $('.modal').modal("hide");
                $('.modal-backdrop').hide();
                if (result.url && result.url.length > 2) {
                    setTimeout(function() {
                        if (result.data == "redirect") {
                            window.location.href = result.url;
                        } else {
                            var tab_html = $(".tab-pane", $("#main_tabs_content"));
                            tab_html.html("");
                            tab_html.load(result.url);
                        }



                    }, 1500);
                } else {

                }
            } else {
                bootoast({
                    message: result.msg,
                    type: 'danger',
                    position: 'right-top',
                    timeout: 2
                });
                if (result.url && result.url.length > 2) {
                    setTimeout(function() {
                        var tab_html = $(".tab-pane", $("#main_tabs_content"));
                        tab_html.html("");
                        tab_html.load(result.url);
                    }, 1500);
                }
            }


        }, 'json');

    });
}

function del_item(_url, _data, _title) {
    if (!_title) {
        _title = "确认删除该数据?";
    }
    bootbox.confirm({
        message: _title,
        buttons: {
            confirm: {
                label: '确认',
                className: 'btn-success'
            },
            cancel: {
                label: '取消',
                className: 'btn-danger'
            }
        },
        callback: function(result) {
            if (result) {
                $.post(_url, _data, function(result) {
                    retrun_fun(result);
                }, "json")
            }

        }
    });
}




function retrun_fun(result) {
    if (result.code == 1) {
        bootoast({
            message: result.msg,
            type: 'success',
            position: 'right-top',
            timeout: 2
        });
        if (result.url && result.url.length > 2) {
            setTimeout(function() {
                window.location.href = result.url;
            }, 1500);
        } else {
            setTimeout(function() {
                window.location.reload();
            }, 1500);
        }
    } else {
        bootoast({
            message: result.msg,
            type: 'danger',
            position: 'right-top',
            timeout: 2
        });
        if (result.url && result.url.length > 2) {
            setTimeout(function() {
                window.location.href = result.url;
            }, 1500);
        }
    }
}




function open_tab(tab_id, url, title) {

    var menu_html = "<li class='active' id='tab_#id#' flag='#id#' url='#url#'><a href='javascript:; '><i class='fa fa-refresh tab_refresh'></i>#title# <i class='fa fa-close tab_close'></a></li>";

    $("#menu_tabs_item li").removeClass("active");
    $("#menu_tabs_item li i").addClass("hide");
    var tab_html = $('<div class="tab-pane active" id="content_tab_' + tab_id + '"></div>');

    menu_html = menu_html.replace(/#id#/g, tab_id).replace(/#title#/g, title).replace(/#url#/g, url);
    menu_html = $(menu_html);
    if ($("#tab_" + tab_id).length > 0) {

        $("#tab_" + tab_id).choose_tab();
        return false;
    }
    $("#main_tabs_content").html("");
    $("#main_tabs_content").html(tab_html);
    $("#menu_tabs_item").append(menu_html);
    tab_html.load(url, function() {
        $(".tab_close", menu_html).on("click", function() {
            $(this).close_tab();
        });
        $(".tab_refresh", menu_html).on("click", function() {
            var refresh = $(this).parent("a").parent("li");
            $(this).refresh_tab();

        });
        menu_html.on("click", function() {
            $(this).choose_tab();
        })

    });
}


function reload_tab(obj) {
    var tab_html = $(".tab-pane", $("#main_tabs_content"));

    var url = $(obj).attr("data-url");
    tab_html.html("");
    tab_html.load(url);
    //tab-pane
}